package ca.liu.scaffoldingmvctest.web;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import ca.liu.scaffoldingmvctest.service.StLogService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for StLog entities
 * 
 */

@Controller("StLogController")
@Scope(WebApplicationContext.SCOPE_SESSION)
public class StLogController {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for StLog entities
	 * 
	 */
	@Autowired
	private StLogService stLogService;

	/**
	 * Show all StUser entities by StLog
	 * 
	 */
	@RequestMapping("/listStLogStUser")
	public ModelAndView listStLogStUser(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", stLogDAO.findStLogByPrimaryKey(idKey));
		mav.setViewName("stlog/stuser/listStUser.jsp");

		return mav;
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@RequestMapping("/saveStLogStUser")
	public ModelAndView saveStLogStUser(@RequestParam Integer stlog_id, @ModelAttribute StUser stuser) {
		StLog parent_stlog = stLogService.saveStLogStUser(stlog_id, stuser);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stlog_id", stlog_id);
		mav.addObject("stlog", parent_stlog);
		mav.setViewName("stlog/viewStLog.jsp");

		return mav;
	}

	/**
	 * Create a new StLog entity
	 * 
	 */
	@RequestMapping("/newStLog")
	public ModelAndView newStLog() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", new StLog());
		mav.addObject("newFlag", true);
		mav.setViewName("stlog/editStLog.jsp");

		return mav;
	}

	/**
	 * Select the child StUser entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStLogStUser")
	public ModelAndView confirmDeleteStLogStUser(@RequestParam Integer stlog_id, @RequestParam Integer related_stuser_id) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", stUserDAO.findStUserByPrimaryKey(related_stuser_id));
		mav.addObject("stlog_id", stlog_id);
		mav.setViewName("stlog/stuser/deleteStUser.jsp");

		return mav;
	}

	/**
	 * Edit an existing StUser entity
	 * 
	 */
	@RequestMapping("/editStLogStUser")
	public ModelAndView editStLogStUser(@RequestParam Integer stlog_id, @RequestParam Integer stuser_id) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(stuser_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stlog_id", stlog_id);
		mav.addObject("stuser", stuser);
		mav.setViewName("stlog/stuser/editStUser.jsp");

		return mav;
	}

	/**
	 * Select an existing StLog entity
	 * 
	 */
	@RequestMapping("/selectStLog")
	public ModelAndView selectStLog(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", stLogDAO.findStLogByPrimaryKey(idKey));
		mav.setViewName("stlog/viewStLog.jsp");

		return mav;
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@RequestMapping("/deleteStLog")
	public String deleteStLog(@RequestParam Integer idKey) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(idKey);
		stLogService.deleteStLog(stlog);
		return "forward:/indexStLog";
	}

	/**
	 * Create a new StUser entity
	 * 
	 */
	@RequestMapping("/newStLogStUser")
	public ModelAndView newStLogStUser(@RequestParam Integer stlog_id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("stlog_id", stlog_id);
		mav.addObject("stuser", new StUser());
		mav.addObject("newFlag", true);
		mav.setViewName("stlog/stuser/editStUser.jsp");

		return mav;
	}

	/**
	 * Show all StLog entities
	 * 
	 */
	@RequestMapping("/indexStLog")
	public ModelAndView listStLogs() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlogs", stLogService.loadStLogs());

		mav.setViewName("stlog/listStLogs.jsp");

		return mav;
	}

	/**
	 * Select the StLog entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStLog")
	public ModelAndView confirmDeleteStLog(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", stLogDAO.findStLogByPrimaryKey(idKey));
		mav.setViewName("stlog/deleteStLog.jsp");

		return mav;
	}

	/**
	 * View an existing StUser entity
	 * 
	 */
	@RequestMapping("/selectStLogStUser")
	public ModelAndView selectStLogStUser(@RequestParam Integer stlog_id, @RequestParam Integer stuser_id) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(stuser_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stlog_id", stlog_id);
		mav.addObject("stuser", stuser);
		mav.setViewName("stlog/stuser/viewStUser.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/stlogController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Entry point to show all StLog entities
	 * 
	 */
	public String indexStLog() {
		return "redirect:/indexStLog";
	}

	/**
	 * Edit an existing StLog entity
	 * 
	 */
	@RequestMapping("/editStLog")
	public ModelAndView editStLog(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", stLogDAO.findStLogByPrimaryKey(idKey));
		mav.setViewName("stlog/editStLog.jsp");

		return mav;
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@RequestMapping("/deleteStLogStUser")
	public ModelAndView deleteStLogStUser(@RequestParam Integer stlog_id, @RequestParam Integer related_stuser_id) {
		ModelAndView mav = new ModelAndView();

		StLog stlog = stLogService.deleteStLogStUser(stlog_id, related_stuser_id);

		mav.addObject("stlog_id", stlog_id);
		mav.addObject("stlog", stlog);
		mav.setViewName("stlog/viewStLog.jsp");

		return mav;
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@RequestMapping("/saveStLog")
	public String saveStLog(@ModelAttribute StLog stlog) {
		stLogService.saveStLog(stlog);
		return "forward:/indexStLog";
	}
}